package com.xjtu.platform.reader.impl;

import com.github.pagehelper.PageInfo;
import com.xjtu.platform.common.PageParam;
import com.xjtu.platform.common.Utils;
import com.xjtu.platform.common.entity.FileEntity;
import com.xjtu.platform.common.entity.PrimaryKey;
import com.xjtu.platform.common.page.QueryAction;
import com.xjtu.platform.common.page.QueryAction2;
import com.xjtu.platform.reader.ReaderService;
import com.xjtu.platform.reader.dao.ReaderDao;
import com.xjtu.platform.reader.dto.BorrowBookDto;
import com.xjtu.platform.reader.dto.BorrowHisDto;
import com.xjtu.platform.reader.dto.GetBookListParamDto;
import com.xjtu.platform.reader.entity.*;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ReaderServiceImpl implements ReaderService {

    @Resource
    private ReaderDao readerDao;

    @Override
    public Map<String,Object> getBookList(GetBookListParamDto dto) {
        return Utils.getPage2(dto, new QueryAction2<BookListEntity>() {
            @Override
            public PageInfo<BookListEntity> execute() {
                try {
                    System.out.println(dto.toString());
                    List<BookBaseInfo> baseList = readerDao.getBookBaseInfoByParam(dto);
                    PageInfo<BookBaseInfo> page = new PageInfo<>(baseList);
                    List<BookListEntity> list = new ArrayList<>();
                    for(BookBaseInfo item : baseList){
                        BookListEntity bookListEntity = new BookListEntity();
                        bookListEntity.setBookBaseInfo(item);

                        List<FileEntity> fileEntity = readerDao.getBookPic(item.getBook_id());
                        for(FileEntity s: fileEntity){
                            s.setFile_url(Utils.transAddToUrl(s.getFile_url()));
                        }
                        bookListEntity.setFileList(fileEntity);

                        List<Author> authorList = readerDao.getBookAuthors(item.getBook_id());
                        bookListEntity.setAuthorList(authorList);

                        List<BookLabel> bookLabelList = readerDao.getBookLabelList(item.getBook_id());
                        bookListEntity.setLabelList(bookLabelList);

                        bookListEntity.setBook_class(readerDao.getBookClass(item.getBook_id()));
                        list.add(bookListEntity);
                    }
                    PageInfo<BookListEntity> page2 = new PageInfo<>();
                    BeanUtils.copyProperties(page,page2);
                    page2.setList(list);
                    return  page2;
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
            }
        });
    }

    @Override
    public boolean borrowBook(BorrowBookDto borrowBookDto, String readerId) {
        try {
            Date date = new Date();
            Timestamp start_time = new Timestamp(date.getTime());
            PrimaryKey primaryKey = new PrimaryKey();
            readerDao.borrowNewBook(borrowBookDto,readerId,start_time,primaryKey);
            String pk_id = primaryKey.getId();
            readerDao.updateBookTotalNum(borrowBookDto);
            System.out.println("pk:"+primaryKey.getId());
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public Map<String, Object> getBorrowHis(BorrowHisDto borrowHisDto) {
        return Utils.getPage2(borrowHisDto, new QueryAction2<BorrowHisEntity>() {
            @Override
            public PageInfo<BorrowHisEntity> execute() {
                List<BorrowHisEntity> list = new ArrayList<>();
                List<BorrowHisBaseInfo> baseList = readerDao.getBorrowHisBaseInfo(borrowHisDto);
                PageInfo<BorrowHisBaseInfo> page = new PageInfo<>(baseList);
                for(BorrowHisBaseInfo item : baseList){
                    BorrowHisEntity entity = new BorrowHisEntity();
                    BookBaseInfo bookBaseInfo = readerDao.getBookBaseInfoById(item.getBook_id());

                    List<FileEntity> fileEntity = readerDao.getBookPic(item.getBook_id());
                    for(FileEntity s: fileEntity){
                        s.setFile_url(Utils.transAddToUrl(s.getFile_url()));
                    }
                    entity.setFileEntities(fileEntity);


                    List<Author> authorList = readerDao.getBookAuthors(item.getBook_id());
                    entity.setAuthorList(authorList);

                    List<BookLabel> bookLabelList = readerDao.getBookLabelList(item.getBook_id());
                    entity.setLabelList(bookLabelList);

                    PenaltyEntity p = readerDao.getPenaltyById(item.getBook_reader_his_id());
                    if(p == null){
                        entity.setPenalty(false);
                    }else {
                        entity.setPenalty(true);
                    }
                    entity.setPenaltyEntity(p);

                    entity.setBook_class(readerDao.getBookClass(item.getBook_id()));

                    entity.setBookBaseInfo(bookBaseInfo);
                    entity.setBorrowHisBaseInfo(item);
                    list.add(entity);
                }
                PageInfo<BorrowHisEntity> page2 = new PageInfo<>();
                BeanUtils.copyProperties(page,page2);
                page2.setList(list);
                return page2;
            }
        });
    }
}

